<?php

class User_orderController extends BaseController {
	public function listAction() {
		$orderStatus = intval(Request::getParams('orderStatus', -1));
		$page = intval(Request::getParams('page', 1));
		$pageSize = intval(Request::getParams('pageSize', 20));
		$uid = $this->user_info['uid'];
		$where = $value = [];
		$where[] = 'is_delete=?';
		$value[] = 0;
		if ($orderStatus != -1&&$orderStatus!=7) {
			if ($orderStatus == 0) {
//待支付
				$where[] = 'o.order_status=?';
				$value[] = $orderStatus;
			} elseif ($orderStatus == 1) {
				//进行中:
				$status = orderModel::ORDER_STATUS_PAIED . ',' . orderModel::ORDER_STATUS_POST . ',' . orderModel::ORDER_STATUS_USER_SIGN . ',' . orderModel::ORDER_STATUS_UNCOMMENT;
				$where[] = "o.order_status in ($status)";
			} elseif ($orderStatus == 2) {
				//已完成
				$where[] = 'o.order_status=?';
				$value[] = orderModel::ORDER_STATUS_FINISH;
			}
		}
		if($orderStatus==7){
            $where[] = 'o.refund_status>?';
            $value[] = 0;
        }
		$where[] = 'o.uid=?';
		$value[] = $uid;
		Response::ajax(200, 'success',
			self::model('order')->getUserOrderList($where, $value, $page, $pageSize));
	}

	public function infoAction() {
		$orderId = Request::getParams('orderId');
		if (!$orderId || !$orderInfo = self::model('order')->getOne('id=?', $orderId)) {
			Response::ajax(NORMAL_ERROR, '参数有误,订单信息查看失败');
		}
		if ($orderInfo['uid'] != $this->user_info['uid']) {
			Response::ajax(NORMAL_ERROR, '参数有误,订单信息查看失败');
		}
		$goodsList = self::model('order_goods')->getAll('order_id=?', $orderId);
		if (!$goodsList) {
			Response::ajax(NORMAL_ERROR, '商品订单查询失败');
		}
		if ($orderInfo['pay_status'] == 0 && $orderInfo['order_status'] == 0) {
			$endtime = strtotime($orderInfo['add_time']) + 3600;
			if ($endtime < time()) {
				self::model('order')->update(array('order_status' => 5), 'id=?', $orderId);
				$orderInfo['pay_status'] = 5;
				//Response::ajax(NORMAL_ERROR, '订单支付超时,请重新下单');
			}
		}
		$specificationIds1 = [];
		$specificationIds2 = [];
		foreach ($goodsList as $key => $value) {
			if ($value['attributes_id1']) {
				$specificationIds1[] = intval($value['attributes_id1']);
				$specificationIds2[] = intval($value['attributes_id2']);
			}
		}
		$specificationIds = array_keys(array_flip($specificationIds1) + array_flip($specificationIds2));
		if ($specificationIds) {
			$specificationIds = implode(',', $specificationIds);
			$attribute = self::model('goods_attribute')->query("select attribute_id,name from wf_goods_attribute where attribute_id in ({$specificationIds})")->fetchAll();
			$attributearr = [];
			foreach ($attribute as $key => $value) {
				$attributearr[$value['attribute_id']] = $value['name'];
			}
		}

		$resultData = [
			'orderId' => intval($orderInfo['id']),
			'title' => strval($orderInfo['title']),
			'image' => strval($orderInfo['image']),
			'orderNo' => strval($orderInfo['order_no']),
			'payStatus' => intval($orderInfo['pay_status']),
			'orderStatus' => intval($orderInfo['order_status']),
			'shopId' => intval($orderInfo['shop_id']),
            'remark' => $orderInfo['remark'],
			'shopInfo' => self::model('shop')->getShopInfo($orderInfo['shop_id']),
			'addTime' => $orderInfo['add_time'],
			'price' => floatval($orderInfo['price']),
			'total_price' => floatval($orderInfo['total_price']),
			'balance' => floatval($orderInfo['balance']),
			'coupon_money' => floatval($orderInfo['coupon_money']),
			'postage' => floatval($orderInfo['postage']),
			'num' => intval($orderInfo['num']),
			'parentId' => intval($orderInfo['parent_id']),
			'addressInfo' => json_decode($orderInfo['address_info'], true),
			'expressCom' => $orderInfo['express_com'],
			'expressNo' => $orderInfo['express_no'],
			'goodsList' => [],
		];
        $resultData['postage']=0;
		foreach ($goodsList as $goodsInfo) {
			if ($goodsInfo['attributes_id1'] && $goodsInfo['attributes_id2']) {
				$attributes1 = $attributearr[$goodsInfo['attributes_id1']];
				$attributes2 = $attributearr[$goodsInfo['attributes_id2']];
				$attributeName = $attributes1 . ' ' . $attributes2;
			} elseif ($goodsInfo['attributes_id1']) {
				$attributes1 = $attributearr[$goodsInfo['attributes_id1']];
				$attributeName = $attributes1;
			} else {
				$attributeName = "";
			}
			$resultData['goodsList'][] = [
				'goodsId' => intval($goodsInfo['goods_id']),
				'name' => $goodsInfo['name'],
				'image' => $goodsInfo['image'],
				'price' => floatval($goodsInfo['price']),
				'num' => intval($goodsInfo['num']),
				'postage' => floatval($goodsInfo['postage']),
				'isComment' => intval($goodsInfo['is_comment']),
				'specification_id' => intval($goodsInfo['specification_id']),
				'attributeName' => $attributeName,
			];
			if($goodsInfo['postage']>$resultData['postage']){
                $resultData['postage'] = $goodsInfo['postage'];
            }

		}
		Response::ajax(200, 'success', $resultData);
	}

	public function deleteAction() {
		$orderId = Request::getParams('orderId');
		if (!$orderId || !$orderInfo = self::model('order')->getOne('id=?', $orderId)) {
			Response::ajax(NORMAL_ERROR, '参数有误,订单信息查看失败');
		}
		if ($orderInfo['uid'] != $this->user_info['uid']) {
			Response::ajax(NORMAL_ERROR, '参数有误,订单信息查看失败');
		}
		if (self::model('order')->update(['is_delete' => 1], 'id=?', $orderId)) {
			Response::ajax(200, '删除成功');
		}
		Response::ajax(NORMAL_ERROR, '删除失败');
	}

	public function finishAction() {
		$orderId = Request::getParams('orderId');
		if (!$orderId || !$orderInfo = self::model('order')->getOne('id=?', $orderId)) {
			Response::ajax(NORMAL_ERROR, '参数有误,订单信息查看失败');
		}
		if ($orderInfo['uid'] != $this->user_info['uid']) {
			Response::ajax(NORMAL_ERROR, '参数有误,订单信息查看失败');
		}
		if ($orderInfo['pay_status'] != orderModel::ORDER_STATUS_PAIED) {
			Response::ajax(NORMAL_ERROR, '抱歉,您的订单未支付,无法评论');
		}
		self::model('order')->update([
			'order_status' => orderModel::ORDER_STATUS_FINISH,
			'finish_time' => time(),
		], 'id=?', $orderId);
		Response::ajax(200, 'success');
	}
}